//
//  ShellChangeLoginPasswordViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellChangeLoginPasswordViewController.h"
#import "ShellAccountTextField.h"
#import "ShellAccountViewModel.h"

@interface ShellChangeLoginPasswordViewController ()

@property (nonatomic,strong)ShellAccountViewModel *viewModel;

@end

@implementation ShellChangeLoginPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = LZLocalizedString(@"修改登录密码");
    
    [self setupView];
    
}

- (void)setupView{
    
    ///!!!:原密码
    ShellAccountTextField *oldPasswordTextField = [[ShellAccountTextField alloc] init];
    oldPasswordTextField.secureTextEntry = YES;
    [self.view addSubview:oldPasswordTextField];
    oldPasswordTextField.secureTextEntry = YES;
    oldPasswordTextField.placeholder = LZLocalizedString(@"原密码");
    [oldPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    ///!!!:新密码
    ShellAccountTextField *newPasswordTextField = [[ShellAccountTextField alloc] init];
    newPasswordTextField.secureTextEntry = YES;
    [self.view addSubview:newPasswordTextField];
    newPasswordTextField.placeholder = LZLocalizedString(@"新密码");
    [newPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(oldPasswordTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    
    ///!!!:确认密码
    ShellAccountTextField *sureNewPasswordTextField = [[ShellAccountTextField alloc] init];
    sureNewPasswordTextField.secureTextEntry = YES;
    [self.view addSubview:sureNewPasswordTextField];
    sureNewPasswordTextField.placeholder = LZLocalizedString(@"重新输入新密码");
    [sureNewPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(newPasswordTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    

    ///!!!:提交
    UIButton *submitBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"提交") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    [submitBtn setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self.view addSubview:submitBtn];
    submitBtn.cornerRadius = 6;
    submitBtn.layer.shadowOffset = CGSizeMake(0,2);
    submitBtn.layer.shadowOpacity = 1;
    submitBtn.layer.shadowRadius = 6;
    submitBtn.enabled = NO;
    [submitBtn setTitleColor:RGBA(255, 255, 255, 0.5) forState:UIControlStateDisabled];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sureNewPasswordTextField.mas_bottom).mas_offset(26);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    

    
    //提交按钮的控制
    [[oldPasswordTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(oldPasswordTextField.text.length > 5
           && newPasswordTextField.text.length > 5
           && [newPasswordTextField.text isEqualToString:sureNewPasswordTextField.text]){
            submitBtn.enabled = YES;
        } else {
            submitBtn.enabled = NO;
        }
    }];
    
    [[newPasswordTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(oldPasswordTextField.text.length > 5
           && newPasswordTextField.text.length > 5
           && [newPasswordTextField.text isEqualToString:sureNewPasswordTextField.text]){
            submitBtn.enabled = YES;
        } else {
            submitBtn.enabled = NO;
        }
    }];
    
    [[sureNewPasswordTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(oldPasswordTextField.text.length > 5
           && newPasswordTextField.text.length > 5
           && [newPasswordTextField.text isEqualToString:sureNewPasswordTextField.text]){
            submitBtn.enabled = YES;
        } else {
            submitBtn.enabled = NO;
        }
    }];
    
    
    @weakify(self);
    [[submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.viewModel changeLoginAccount:oldPasswordTextField.text password:newPasswordTextField.text];
    }];
}



//viewModel
- (ShellAccountViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ShellAccountViewModel alloc] init];
    }
    return _viewModel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
